package dk.muldbjergskolen.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import dk.muldbjergskolen.ctr.InventarCtr;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OpretInventar extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField_Navn;
	private JTextField textField_Beskrivelse;
	private JTextField textField_Antal;
	private InventarCtr iC = new InventarCtr();
	private JFrame frame;


	/**
	 * Create the dialog.
	 */
	public OpretInventar(OpretInventarListener listener) {
		opretInventarListener = listener;
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Opret inventar");
		setBounds(100, 100, 214, 174);
		getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNavn = new JLabel("Navn:");
			lblNavn.setBounds(10, 14, 57, 14);
			contentPanel.add(lblNavn);
		}
		{
			JLabel lblAntal = new JLabel("Antal:");
			lblAntal.setBounds(10, 42, 57, 14);
			contentPanel.add(lblAntal);
		}
		{
			JLabel lblBeskrivelse = new JLabel("Beskrivelse:");
			lblBeskrivelse.setBounds(10, 70, 57, 14);
			contentPanel.add(lblBeskrivelse);
		}
		
		textField_Navn = new JTextField();
		textField_Navn.setBounds(102, 11, 86, 20);
		contentPanel.add(textField_Navn);
		textField_Navn.setColumns(10);
		
		textField_Antal = new JTextField();
		textField_Antal.setBounds(102, 39, 86, 20);
		contentPanel.add(textField_Antal);
		textField_Antal.setColumns(10);
		
		textField_Beskrivelse = new JTextField();
		textField_Beskrivelse.setBounds(102, 67, 86, 20);
		contentPanel.add(textField_Beskrivelse);
		textField_Beskrivelse.setColumns(10);
		
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						opret();
					}
				
				
			
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
	}
			
	

	//
	// Event håndtering.
	//

	// "Lyttere"
	OpretInventarListener opretInventarListener;

	public interface OpretInventarListener {
		void OnNewInventar();
	}

	/***
	 * Kaldes når en ny Lokale er lavet.
	 */
	private void OnNewInventar() {
		if (opretInventarListener != null)
			opretInventarListener.OnNewInventar();
	}
	
	// opret inventar
	public void opret()
	{
		try
		{
		String navn = textField_Navn.getText();
		String beskrivelse  = textField_Beskrivelse.getText();
		int antal = Integer.parseInt(textField_Antal.getText());
		iC.opretInventar(navn, beskrivelse, antal);
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(frame, "Udfyld alle Felterne");
			return;
		}
		OnNewInventar();
		setVisible(false);
	}
	
}
